class Solution(object):
    def minimumOperations(self, nums, start, goal):
        if start == goal:
            return 0
        d = {start}
        now = {start}
        dist = 0
        while now:
            dist += 1
            next_set = set()
            for point in now:
                for num in nums:
                    if point + num not in d:
                        if point + num == goal:
                            return dist
                        d.add(point + num)
                        if 0 <= point + num <= 1000:
                            next_set.add(point + num)
                    if point - num not in d:
                        if point - num == goal:
                            return dist
                        d.add(point - num)
                        if 0 <= point - num <= 1000:
                            next_set.add(point - num)
                    if point ^ num not in d:
                        if point ^ num == goal:
                            return dist
                        d.add(point ^ num)
                        if 0 <= point ^ num <= 1000:
                            next_set.add(point ^ num)
            now = next_set
        return -1

